package com.blog.service;

import com.blog.mappers.BlogMapper;
import com.blog.pojo.Article;
import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

/**
 * Created with IntelliJ IDEA by ChouFy on 2020-03-23.
 *
 * @author zhoufy
 */
@Service
public class BlogService {


    @Autowired
    private BlogMapper blogMapper;

    private final Integer pageSize = 2;


    public List<Article> getList(int page) {
        page = page * pageSize;


        Example example = new Example(Article.class);
        List<Article> articles = blogMapper.selectByExampleAndRowBounds(example, new RowBounds(page, pageSize));
        return articles;
    }


    public int pageCount(){
        Example example = new Example(Article.class);

        int i = blogMapper.selectCountByExample(example);
        return i % pageSize == 0 ? i % pageSize : i % pageSize +1;

    }
}
